
#ifndef I2C_H
#define I2C_H

#include <xc.h> // include processor files - each processor file is guarded.
#include "portdefine.h"
#include "stdint.h"
// TODO Insert appropriate #include <>

// TODO Insert C++ class definitions if appropriate

// TODO Insert declarations
// #define _XTAL_FREQ      32000000
// #define I2C_BaudRate    100000
/*
#define IIC_SCL         LATCbits.LATC3
#define IIC_SDA         LATCbits.LATC4
#define READ_SDA        PORTCbits.RC4

#define SDA_IN()        TRISCbits.TRISC4 = 1//SDA1 in

#define SDA_OD_OUT()    TRISCbits.TRISC4 = 0//SDA1 output
#define SCL_OD_OUT()    TRISCbits.TRISC3 = 0//SCL1 output


#define     SCL_DOWN    (LATCbits.LATC3 = 0)
#define     SCL_UP      { LATCbits.LATC3 = 1;\
                          uint32_t timeout = 10000;  \
                          while(timeout && (PORTCbits.RC3 == 0)) timeout --; }
#define     SDA_DOWN    (LATCbits.LATC4 = 0)
#define     SDA_UP      (LATCbits.LATC4 = 1)

#define     SDA_OUT     TRISCbits.TRISC4 = 0//SDA1 output
#define     SDA_IN      TRISCbits.TRISC4 = 1//SDA1 in
#define     READ_SDA    PORTCbits.RC4






void i2c_sw_init(void);
void i2c_sw_start(void);
void i2c_sw_stop(void);
void i2c_sw_ack(void);
void i2c_sw_nack(void);
uint8_t i2c_sw_wait_ack(void);
void i2c_sw_write_byte(uint8_t data);
uint8_t i2c_sw_read_byte(void);
uint8_t i2c_sw_write_registers(uint8_t slave_addr, uint8_t register_addr, uint8_t *buff, uint8_t len);
uint8_t i2c_sw_read_registers(uint8_t slave_addr, uint8_t register_addr, uint8_t *buff, uint8_t len);
uint16_t bq20z90GetTemp();
uint8_t bq20z90GetCapacity();
*/
#define IIC_SCL1 LATCbits.LATC3
#define IIC_SDA1 LATCbits.LATC4
#define READ_SDA1 PORTCbits.RC4

#define SDA1_IN() TRISCbits.TRISC4 = 1 // SDA1 in

#define SDA1_OD_OUT() TRISCbits.TRISC4 = 0 // SDA1 output
#define SCL1_OD_OUT() TRISCbits.TRISC3 = 0 // SCL1 output

#define SCL1_DOWN (LATCbits.LATC3 = 0)
#define SCL1_UP                                 \
    {                                           \
        LATCbits.LATC3 = 1;                     \
        uint32_t timeout = 10000;               \
        while (timeout && (PORTCbits.RC3 == 0)) \
            timeout--;                          \
    }
#define SDA1_DOWN (LATCbits.LATC4 = 0)
#define SDA1_UP (LATCbits.LATC4 = 1)

#define SDA1_OUT TRISCbits.TRISC4 = 0 // SDA1 output
#define SDA1_IN TRISCbits.TRISC4 = 1  // SDA1 in
#define READ_SDA1 PORTCbits.RC4

#define IIC_SCL2 LATDbits.LATD6
#define IIC_SDA2 LATDbits.LATD5
#define READ_SDA2 PORTDbits.RD5

#define SDA2_IN() TRISDbits.TRISD5 = 1 // SDA2 in

#define SDA2_OD_OUT() TRISDbits.TRISD5 = 0 // SDA2 output
#define SCL2_OD_OUT() TRISDbits.TRISD6 = 0 // SCL2 output

#define SCL2_DOWN (LATDbits.LATD6 = 0)
#define SCL2_UP                                 \
    {                                           \
        LATDbits.LATD6 = 1;                     \
        uint32_t timeout = 10000;               \
        while (timeout && (PORTDbits.RD6 == 0)) \
            timeout--;                          \
    }
#define SDA2_DOWN (LATDbits.LATD5 = 0)
#define SDA2_UP (LATDbits.LATD5 = 1)

#define SDA2_OUT TRISDbits.TRISD5 = 0 // SDA2 output
#define SDA2_IN TRISDbits.TRISD5 = 1  // SDA2 in
#define READ_SDA2 PORTDbits.RD5

void i2c1_sw_init(void);
void i2c1_sw_start(void);
void i2c1_sw_stop(void);
void i2c1_sw_ack(void);
void i2c1_sw_nack(void);
uint8_t i2c1_sw_wait_ack(void);
void i2c1_sw_write_byte(uint8_t data);
uint8_t i2c1_sw_read_byte(void);
uint8_t i2c1_sw_write_registers(uint8_t slave_addr, uint8_t register_addr, uint8_t *buff, uint8_t len);
uint8_t i2c1_sw_read_registers(uint8_t slave_addr, uint8_t register_addr, uint8_t *buff, uint8_t len);
uint16_t LeftBatGetTemp();
uint8_t LeftBatGetCapacity();
uint8_t Init1LM73();
uint16_t LM73Read1();

void i2c2_sw_init(void);
void i2c2_sw_start(void);
void i2c2_sw_stop(void);
void i2c2_sw_ack(void);
void i2c2_sw_nack(void);
uint8_t i2c2_sw_wait_ack(void);
void i2c2_sw_write_byte(uint8_t data);
uint8_t i2c2_sw_read_byte(void);
uint8_t i2c2_sw_write_registers(uint8_t slave_addr, uint8_t register_addr, uint8_t *buff, uint8_t len);
uint8_t i2c2_sw_read_registers(uint8_t slave_addr, uint8_t register_addr, uint8_t *buff, uint8_t len);
uint16_t RightBatGetTemp();
uint8_t RightBatGetCapacity();
uint8_t Init2LM73();
uint16_t LM73Read2();
#endif /* XC_HEADER_TEMPLATE_H */
